package com.hxf.service.meal.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.hxf.common.PublicUtil;
import com.hxf.dto.MealMenuDto;
import com.hxf.dto.RestaurantDto;
import com.hxf.dto.ReturnData;
import com.hxf.dto.UserDto;
import com.hxf.exception.DaoLevelException;
import com.hxf.exception.ServiceException;
import com.hxf.service.base.impl.BaseServiceImpl;
import com.hxf.service.meal.RestaurantService;

@Repository("restaurantService")
public class RestaurantServiceImpl extends BaseServiceImpl implements RestaurantService {

	@Override
	public List<RestaurantDto> findByRestaurant(Integer userId) throws ServiceException {
		 
		try {
			return super.daoFacade.getRestaurantDao().findByRestaurant(userId);
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}
	
	@Override
	public ReturnData<RestaurantDto> returnDataRestaurant(Integer userId) throws ServiceException {
		 
		try {
			return (ReturnData<RestaurantDto>) super.daoFacade.getRestaurantDao().findByRestaurant(userId);
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}
	
	@Override
	public List<RestaurantDto> findByRestaurantId(Integer userId) throws ServiceException{
		try{
			return (List<RestaurantDto>)super.daoFacade.getRestaurantDao().findByRestaurantId(userId);
		}catch(DaoLevelException e){
			e.getMessage();
			throw new ServiceException(e.getMessage(),e);
		}
	}

	@Override
	public ReturnData<RestaurantDto> findByRestaurantAllPage(int targetPage,
			int pageSize) throws ServiceException {
		try {
			return super.daoFacade.getRestaurantDao().findByRestaurantAllPage(targetPage, pageSize);
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		} 
	}

	@Override
	public int updateRestaurant(String ids, Integer status) throws ServiceException {
		try { 
			if (PublicUtil.isNotNullAndEmpty(ids) && status != null) {
				String [] id  = ids.split(",");
				RestaurantDto rdto = null;
				for (String rid : id) {
					rdto = (RestaurantDto)super.daoFacade.getRestaurantDao().get(RestaurantDto.class, Integer.parseInt(rid)); 
					rdto.setStatus(status);
					super.update(rdto);
				}
			} else {
				return -1;
			}
			return 0;
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e); 
		}
	}
	
	public int updateRestaurantRecommend(String ids, boolean isRecommend) throws ServiceException {
		try { 
			if (PublicUtil.isNotNullAndEmpty(ids)) {
				String [] id  = ids.split(",");
				RestaurantDto rdto = null;
				for (String rid : id) {
					rdto = (RestaurantDto)super.daoFacade.getRestaurantDao().get(RestaurantDto.class, Integer.parseInt(rid)); 
					rdto.setIsRecommend(isRecommend);
					super.update(rdto);
				}
			} else {
				return -1;
			}
			return 0;
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e); 
		}
	}
	
	/*
	 * 餐厅管理 查询 餐厅名
	 */
	public ReturnData<RestaurantDto> findByRestaurantSelectName(RestaurantDto restDto,int targetPage,
			int pageSize) throws ServiceException{
		
		try{
			return super.daoFacade.getRestaurantDao().findByRestaurantSelectName(restDto, targetPage, pageSize);
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public List<RestaurantDto> findByRestaurantName(RestaurantDto restDto) throws ServiceException{
		try{
			return (List<RestaurantDto>)super.daoFacade.getRestaurantDao().findByRestaurantName(restDto);
		}catch(DaoLevelException e){
			e.getMessage();
			throw new ServiceException(e.getMessage(),e);
		}
	}
}
